<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 后台菜单管理
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Category extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		$this->adminCurrentTemplate = $this->JayphaDB->getConfigValue('adminCurrentTemplate');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());		
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = "category";
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['disabled'] = $this->input->post('disabled');
			$data['channelId'] = $this->input->post('channelId');
			$data['parentId'] = $this->input->post('parentId');
			$data['attribute'] = $this->input->post('attribute');
			$data['title'] = $this->input->post('title');			
		}else{
			$data['disabled'] = $this->input->get('disabled');
			$data['channelId'] = $this->input->get('channelId');
			$data['parentId'] = $this->input->get('parentId');
			$data['attribute'] = $this->input->get('attribute');
			$data['title'] = $this->input->get('title');	
		}
		$data['title'] = urldecode($data['title']);
		$condition = array();
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['disabled']>0)){
			$condition['disabled'] = $data['disabled'];
		}
		if(strlen($data['channelId'])>0){
			$condition['channelId'] = $data['channelId'];
		}
		if(strlen($data['parentId'])>0){
			$condition['parentId'] = $data['parentId'];
		}
		if(strlen($data['attribute'])>0){
			$condition['attribute'] = $data['attribute'];
		}
		if(strlen($data['title'])>0){
			array_push($db_like_key,'upper(title)');
			array_push($db_like_value,strtoupper($data['title']));
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}

		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;

		if($this->input->server('REQUEST_METHOD')=='POST'){
			switch(strtoupper($active)){
				case 'DEL':					
					//删除
					$this->db->trans_start();				
					if(strlen($del)==0){
						//删除所选
						$condition['db_where_in'] = array('id',$id);
						if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('db_where_in'=>array('parentId',$id)))>0){
							unset($data['errorScript']['lhgdialog_alert_mess_top']);
							$this->db->trans_rollback();							
							$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
									'width'=>360,
									'height'=>108,
									'title'=>'删除错误',
									'content'=>'<span class="red">对不起，您所选择的类别中，存在子类，请先删除子类！</span>',
									'url'=>'history.back();',
									'target'=>'_self',
									'conver'=>false,
									'resize'=>false,
									'drag'=>false,
									'btns'=>true,
									'btnText'=>'返回',
									'rightClose'=>false
							));
							$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
							return false;
						}
						//$this->db->trans_rollback();
						$tableList = array('tableName'=>$this->dbTablePrefix.$tvName,
										'db_join'=>array(0=>array('tableName'=>$this->dbTablePrefix.'channel',
														'conditions'=>$this->dbTablePrefix.$tvName.'.channelId='.$this->dbTablePrefix.'channel.id'))
										);
						$whereList['db_where_in'] = array($this->dbTablePrefix.$tvName.'.id',$id);
						$subjoinTable = $this->JayphaDB->getDB(0,0,'subjoinTable',$tableList,$whereList,'',true);	
						$subjoin_table = array();
						if(is_array($subjoinTable)){
							if(count($subjoinTable)>0){
								for($i=0;$i<count($subjoinTable);$i++){
									$subjoin_table[$i] = $subjoinTable[$i]->subjoinTable;
								}
							}
						}
						unset($tableList);
						unset($whereList);
						unset($subjoinTable);
						if(count($subjoin_table)>0){
							foreach($subjoin_table as $table){
								$this->JayphaDB->delDB($this->dbTablePrefix.$table,array('db_where_in'=>array('categoryId',$id)));
							}
						}
						unset($subjoin_table);
						$this->JayphaDB->delDB($this->dbTablePrefix.'archives',array('db_where_in'=>array('categoryId',$id)));
					}else{
						//删除满足搜索条件的全部
						//$this->db->trans_rollback();
						$channelIdList = $this->JayphaDB->getDB(0,0,'channelId',$this->dbTablePrefix.$tvName,$condition,'',true);						
						$subjoin_table = array();
						if(is_array($channelIdList)){
							if(count($channelIdList)>0){
								for($i=0;$i<count($channelIdList);$i++){
									$subjoin_table[$i] = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.'channel',array('id'=>$channelIdList[$i]->channelId),'');
									$this->JayphaDB->delDB($this->dbTablePrefix.$subjoin_table[$i],array('db_where_in'=>array('channelId',$channelIdList[$i]->channelId)));
									$this->JayphaDB->delDB($this->dbTablePrefix.'archives',array('db_where_in'=>array('channelId',$channelIdList[$i]->channelId)));
								}
							}
						}
						unset($subjoin_table);
					}
					$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$condition);
					$this->db->trans_complete();
					break;
				case 'REMOVE':
					//移动
					$this->db->trans_start();
					$condition2['db_where_in'] = array('id',$id);	
					$parentId = $this->input->post('parentId');
					if(in_array($parentId,$id)){
						unset($data['errorScript']['lhgdialog_alert_mess_top']);
						$this->db->trans_rollback();							
						$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
								'width'=>380,
								'height'=>108,
								'title'=>'移动错误',
								'content'=>'<span class="red">对不起，您所选择的类别中，包含目标类型，请将其取消选择！</span>',
								'url'=>'history.back();',
								'target'=>'_self',
								'conver'=>false,
								'resize'=>false,
								'drag'=>false,
								'btns'=>true,
								'btnText'=>'返回',
								'rightClose'=>false
						));
						$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
						return false;
					}
					$this->JayphaDB->updateDB(array('parentId'=>$parentId),$this->dbTablePrefix.$tvName,$condition2);
					$this->db->trans_complete();
					break;
				case 'DISABLED':
					//启用状态
					$condition2['db_where_in'] = array('id',$id);
					$disabled = $this->input->post('disabled');
					$this->JayphaDB->updateDB(array('disabled'=>$disabled),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'NEWEST':
					//最新状态
					$condition2['db_where_in'] = array('id',$id);
					$newest = $this->input->post('newest');
					$this->JayphaDB->updateDB(array('newest'=>$newest),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'TOP':
					//置顶状态
					$condition2['db_where_in'] = array('id',$id);
					$top = $this->input->post('top');
					$this->JayphaDB->updateDB(array('top'=>$top),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'RECOMMEND':
					//推荐状态
					$condition2['db_where_in'] = array('id',$id);
					$recommend = $this->input->post('recommend');
					$this->JayphaDB->updateDB(array('recommend'=>$recommend),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'SHOWHOME':
					//首页显示状态
					$condition2['db_where_in'] = array('id',$id);
					$showhome = $this->input->post('showhome');
					$this->JayphaDB->updateDB(array('showhome'=>$showhome),$this->dbTablePrefix.$tvName,$condition2);
					break;
			}
		}

		unset($condition);

		$config['base_url'] = site_url("admin/category/index");
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';

		$this->pagination->initialize($config);

		$data['currentSite'] = '频道管理<span class="split">>></span>网站栏目管理<span class="split">>></span>栏目列表';
		$categoryArr = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.$tvName,'','parentId,id',false));
		$data['categoryAll'] = $this->JayphaDB->getChildrenList($categoryArr,0,1);
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = '网站栏目管理';
		$data['editPage'] = site_url('admin/category/edit');
		$data['editTitle'] = '修改网站栏目';
		$data['addTitle'] = '添加网站栏目';
		$data['perm'] = 'category';
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'parentId,id';
		$num = $this->uri->segment(4,0);
		$data['channelList'] = $this->JayphaDB->getDB(0,0,'id,title',$this->dbTablePrefix.'channel',array('disabled'=>0),'',false);
		$data['categoryList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		foreach($data['categoryList'] as $category){
			if($category->parentId!=0){
				$category->parentTitle = $this->JayphaDB->getDBValue('title',$this->dbTablePrefix.$tvName,array('id'=>$category->parentId),'');
			}else{
				$category->parentTitle = '顶级分类';
			}
			if($category->channelId==0){
				$category->channelTitle = '未分配';
			}else{
				$category->channelTitle = $this->JayphaDB->getDBValue('title',$this->dbTablePrefix.'channel',array('id'=>$category->channelId),'');
			}
		}
		$this->load->view("admin/$this->adminCurrentTemplate/category",$data);
	}
	
	function edit(){
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'category';
		$data['tvName'] = $tvName;
		$data['perm'] = 'category';
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$data['id'] = $this->input->post('id');
			$data['parentId'] = $this->input->post('parentId');
			$data['disabled'] = $this->input->post('disabled');
			$data['channelId'] = $this->input->post('channelId');
			$data['oIndex'] = $this->input->post('oIndex');
			$data['title'] = $this->input->post('title');
			$data['seotitle'] = $this->input->post('seotitle');
			$data['folder'] = $this->input->post('folder');
			$data['usePinYin'] = $this->input->post('usePinYin');
			$data['attribute'] = $this->input->post('attribute');
			$data['keywords'] = $this->input->post('keywords');
			$data['description'] = $this->input->post('description');
			$data['tempIndex'] = $this->input->post('tempIndex');
			$data['tempList'] = $this->input->post('tempList');
			$data['tempArticle'] = $this->input->post('tempArticle');
			$data['content'] = $this->input->post('content');
			$data['childrenExtends'] = $this->input->post('childrenExtends');
			if($data['parentId']==0){
				$data['path'] = '/';
			}else{
				$data['path'] = $this->JayphaDB->getDBValue('path',$this->dbTablePrefix.$tvName,array('id'=>$data['parentId']),'');
				$data['path'] .= $this->JayphaDB->getDBValue('folder',$this->dbTablePrefix.$tvName,array('id'=>$data['parentId']),'').'/';
			}
			if(strlen($data['childrenExtends'])==0) 
				$data['childrenExtends'] = 0;
			else
				$data['childrenExtends'] = (bool)$data['childrenExtends'];
			if(strlen($data['usePinYin']==0))
				$data['usePinYin'] = 0;
			if(strlen($data['title'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>280,
						'height'=>108,
						'title'=>'名称错误',
						'content'=>'<span class="red">对不起，名称不能为空，请输入名称！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if($data['usePinYin'])
				$data['folder'] = getPinyin($data['title']);
			if(strlen($data['id'])==0){
				//添加
				$insertData = array(
					'disabled'=>$data['disabled'],
					'parentId'=>$data['parentId'],
					'channelId'=>$data['channelId'],
					'oIndex'=>$data['oIndex'],
					'title'=>$data['title'],
					'seotitle'=>$data['seotitle'],
					'path'=>$data['path'],
					'folder'=>$data['folder'],
					'attribute'=>$data['attribute'],
					'keywords'=>$data['keywords'],
					'description'=>$data['description'],
					'tempIndex'=>$data['tempIndex'],
					'tempList'=>$data['tempList'],
					'tempArticle'=>$data['tempArticle'],
					'content'=>$data['content']
				);
				$condition = array('upper(title)'=>strtoupper($data['title']),
								'channelId'=>$data['channelId'],
								'parentId'=>$data['parentId']);
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$condition)>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>320,
							'height'=>108,
							'title'=>'添加失败',
							'content'=>'<span class="red">对不起，该名称已经存在，请选择其它名称！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				
				$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$insertData,true);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>260,
						'height'=>108,
						'title'=>'添加成功 ',
						'content'=>'您已成功添加一项网站栏目！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
				
			}else{
				//修改
				$where = array('id'=>$data['id']);
				$joinTable[0] = array('tableName'=>$this->dbTablePrefix.'channel',
								'conditions'=>$this->dbTablePrefix.$tvName.'.channelId='.$this->dbTablePrefix.'channel.id',
								'joinType'=>'inner');
				$join_table = array('tableName'=>$this->dbTablePrefix.$tvName,
								'db_join'=>$joinTable);
				$old_category = $this->JayphaDB->getDB(0,1,'mainTable,subjoinTable',$join_table,array($this->dbTablePrefix.$tvName.'.id'=>$data['id']),'',false);
				$mainTable = $old_category[0]->mainTable;
				$subjoinTable = $old_category[0]->subjoinTable;
				$updateData = array(
					'disabled'=>$data['disabled'],
					'parentId'=>$data['parentId'],
					'channelId'=>$data['channelId'],
					'oIndex'=>$data['oIndex'],
					'title'=>$data['title'],
					'seotitle'=>$data['seotitle'],
					'path'=>$data['path'],
					'folder'=>$data['folder'],
					'attribute'=>$data['attribute'],
					'keywords'=>$data['keywords'],
					'description'=>$data['description'],
					'tempIndex'=>$data['tempIndex'],
					'tempList'=>$data['tempList'],
					'tempArticle'=>$data['tempArticle'],
					'content'=>$data['content']
				);
				$condition = array('id != '=>$data['id'],
								'upper(title)'=>strtoupper($data['title']),
								'channelId'=>$data['channelId'],
								'parentId'=>$data['parentId']);
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$condition)>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>320,
							'height'=>108,
							'title'=>'修改失败',
							'content'=>'<span class="red">对不起，该名称已经存在，请选择其它名称！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$this->db->trans_start();
				if($data['childrenExtends']==1){
					$childrenUpDate = array('tempIndex'=>$data['tempIndex'],
									'tempList'=>$data['tempList'],
									'tempArticle'=>$data['tempArticle']);
					$this->JayphaDB->updateDB($childrenUpDate,$this->dbTablePrefix.$tvName,array('parentId'=>$data['id']));
				}
				$this->JayphaDB->updateDB(array('channelId'=>$data['channelId']),$this->dbTablePrefix.$tvName,array('parentId'=>$data['id']));
				$this->JayphaDB->updateDB(array('channelId'=>$data['channelId']),$this->dbTablePrefix.$mainTable,array('categoryId'=>$data['id']));
				$this->JayphaDB->updateDB(array('channelId'=>$data['channelId']),$this->dbTablePrefix.$subjoinTable,array('categoryId'=>$data['id']));	
				$this->JayphaDB->updateDB($updateData,$this->dbTablePrefix.$tvName,$where);
				$this->db->trans_complete();
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>260,
						'height'=>108,
						'title'=>'修改成功 ',
						'content'=>'您的网站栏目已成功修改！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}	
		}else{
			$data['id'] = $this->uri->segment(4);
			if(strlen($data['id'])==0){
				$data['parentId'] = 0;
				$data['disabled'] = 0;
				$data['channelId'] = 0;
				$data['oIndex'] = 50;
				$data['title'] = '';
				$data['seotitle'] = '';
				$data['folder'] = '';
				$data['attribute'] = '';
				$data['keywords'] = '';
				$data['description'] = '';
				$data['tempIndex'] = 'index_article';
				$data['tempList'] = 'list_article';
				$data['tempArticle'] = 'content_article';
				$data['content'] = '';
			}else{
				$category = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'',false);
				if(count($category)!=0){
					foreach($category as $row){
						$data['disabled'] = $row->disabled;
						$data['parentId'] = $row->parentId;
						$data['channelId'] = $row->channelId;
						$data['oIndex'] = $row->oIndex;
						$data['title'] = $row->title;
						$data['seotitle'] = $row->seotitle;
						$data['folder'] = $row->folder;
						$data['attribute'] = $row->attribute;
						$data['keywords'] = $row->keywords;
						$data['description'] = $row->description;
						$data['tempIndex'] = $row->tempIndex;
						$data['tempList'] = $row->tempList;
						$data['tempArticle'] = $row->tempArticle;
						$data['content'] = $row->content;
					}
				}else{
					$data['disabled'] = 0;
					$data['parentId'] = 0;
					$data['channelId'] = 0;
					$data['oIndex'] = 50;
					$data['title'] = '';
					$data['seotitle'] = '';
					$data['folder'] = '';
					$data['attribute'] = '';
					$data['keywords'] = '';
					$data['description'] = '';
					$data['tempIndex'] = 'index_article';
					$data['tempList'] = 'list_article';
					$data['tempArticle'] = 'content_article';
					$data['content'] = '';
				}
			}
		}
		$data['channelList'] = $this->JayphaDB->getDB(0,0,'id,title',$this->dbTablePrefix.'channel',array('disabled'=>0),'',false);
		$categoryAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.$tvName,array('disabled'=>0),'parentId,id',false));
		$data['categoryAll'] = $this->JayphaDB->getChildrenList($categoryAll,0,1);
		
		$this->CKEditor = new CKEditor();
		$this->CKEditor->basePath = base_url().$this->config->item('CKEditor_path');
		$this->CKEditor->config['height'] = 126;
		$this->CKEditor->config['toolbar'] = 'full';
		$this->CKEditor->config['language'] = 'zh-cn';
		$this->CKEditor->config['toolbarStartupExpanded'] = true;
		CKFinder::SetupCKEditor($this->CKEditor,$this->config->item('CKFinder_path'));
		$this->load->view("admin/$this->adminCurrentTemplate/categoryEdit",$data);		
	}
}

/* End of file category.php */
/* Location: ./application/controllers/admin/category.php */